﻿using System.Linq;
using System.Web.Mvc;
using AidBrella.BLL;
using AidBrella.ViewModels;

namespace AidBrella.Controllers
{
    [Authorize]
    public class FeedsController : Controller
    {
       private readonly ProjectModel _projectModel = new ProjectModel();
       private readonly UserModel _userModel = new UserModel();

        

        /// <summary>
        /// Returns Feeds of both own projects and "follow" projects
        /// </summary>
        /// <returns></returns>
        public virtual JsonResult GetFeedForUser()
        {
            var userId = _userModel.GetUserId(HttpContext.User.Identity.Name);
            var service = new ProjectViewModelService();
            var userOwnedProject = service.GetProjectsUserHasCreated(userId);
            var projectsUserfollow = service.GetProjectsUserIsFollowing(userId);
            var feeds = userOwnedProject.Select(ownProject => service.GetFeedsByProject(ownProject.Id)).ToList();
            feeds.AddRange(projectsUserfollow.Select(followProject => service.GetFeedsByProject(followProject.Id)));

            return Json(feeds);
        }


        /// <summary>
        /// Returns Feeds for requested projectId
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public virtual JsonResult GetProjectFeed(int projectId)
        {
            var service = new ProjectViewModelService();
            var feeds = service.GetFeedsByProject(projectId);
            return Json(feeds, JsonRequestBehavior.AllowGet);
        }

        

       /// <summary>
       /// Posts status message on a project wall
       /// </summary>
       /// <param name="projectId"></param>
       /// <param name="sts"></param>
       /// <returns></returns>
        public ActionResult UpdateStatus(int projectId, string sts)
        {
           
            var userId = _userModel.GetUserId(HttpContext.User.Identity.Name);

            _projectModel.UpdateStatus(projectId, sts, userId);

            return Json(new { });
        }


        /// <summary>
        /// Posts Comment on a status message
        /// </summary>
        /// <param name="feedId"></param>
        /// <param name="comment"></param>
        /// <returns></returns>
        public  JsonResult CommentOnFeed(int feedId, string comment)
        {
           var userId = _userModel.GetUserId(HttpContext.User.Identity.Name);

            _projectModel.CommentOnFeed(feedId, comment, userId);
            return Json(new {});
        }

    }


   
}
